package com.bsi.com.bsi.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.authentication.logout.LogoutHandler;

import java.util.Arrays;

/**
 * @ClassName WebSercurityInitConfig
 * @Author LiuSong
 * @Date 2020/8/11 17:50
 * @Version 1.0
 * @Description
 */
@Configuration
public class WebSercurityInitConfig extends WebSecurityConfigurerAdapter {
//
//    @Bean
//    @Override
//    public UserDetailsService userDetailsService() {
//        InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager();
//        userDetailsManager.createUser(User.withUsername("zhangsan").password("$2a$10$RyIJhFF0FMurMs.5mNxIy.IP3omfwsfwMTybikITjA6pwvVTgshHm").authorities("r1").build());
//        userDetailsManager.createUser(User.withUsername("lisi").password("$2a$10$RyIJhFF0FMurMs.5mNxIy.IP3omfwsfwMTybikITjA6pwvVTgshHm").authorities("r2").build());
//        return userDetailsManager;
//    }


    /**
     * 返回自定义的userDetailsService
     *
     * @return
     */
    @Override
    @Bean
    public UserDetailsService userDetailsService() {
        return new OvitUserDetailsService();
    }


//    @Bean
//    public PasswordEncoder passwordEncoder() {
//        return NoOpPasswordEncoder.getInstance();
//    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

//    @Bean
//    public LogoutHandler logoutHandler() {
//        return new LogoutHandlerEx();
//    }

    @Override
    public void configure(HttpSecurity http) throws Exception {

//      此策略session不生效不创建，访问接口必须得先登录
//      http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        http.csrf().and()
                .authorizeRequests()
                .antMatchers("/r/r1").hasAuthority("p1")
                .antMatchers("/r/r2").hasAuthority("p2").
                antMatchers("/r/**").authenticated()
                .anyRequest().permitAll()
                .and().formLogin().successForwardUrl("/login-success").and().logout()
//                .addLogoutHandler(logoutHandler())
                .logoutUrl("/logout").logoutSuccessUrl("/login");
    }
}
